package jmu.ant.task.time ;


import java.text.DateFormat ;
import java.text.MessageFormat ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Locale ;

import org.apache.tools.ant.Task ;

public class TimeTask extends Task
{
    private static final String DEFAULT_LOCALE = "en" ;
    private static final String DEFAULT_FORMAT = "dd.MM.yyyy - hh:mm:ss" ;

    private String locale ;
    private String pattern ;
    private String message ;

    public TimeTask ()
    {
        setTaskName ( "time" ) ;
    }

    @Override
    public void execute ()
    {
        // validate locale
        if ( null == locale || locale.isEmpty () )
        {
            setLocale ( DEFAULT_LOCALE ) ;
        }

        // print formatted string
        System.out.println ( MessageFormat.format ( message, createFormatter ().format ( new Date () ) ) ) ;
    }

    public void setLocale ( String locale )
    {
        this.locale = locale ;
    }

    public void setMessage ( String message )
    {
        this.message = message ;
    }

    public void setPattern ( String pattern )
    {
        this.pattern = pattern ;
    }

    private DateFormat createFormatter ()
    {
        try
        {
            return new SimpleDateFormat ( pattern, new Locale ( locale ) ) ;
        }
        catch ( final Exception e )
        {
            return new SimpleDateFormat ( DEFAULT_FORMAT, new Locale ( DEFAULT_LOCALE ) ) ;
        }
    }
}
